package ru.mcfr.oxygen.framework.listeners;

import ro.sync.ecss.extensions.api.*;

/**
 * Simple Documentation Framework Author listener.
 */
public class MOFAuthorListener extends AuthorListenerAdapter {

    /**
     * Access to the author specific functions.
     */
    private AuthorAccess authorAccess;
    private final AuthorDocumentController controller;

    /**
     * Constructor.
     *
     * @param authorAccess Access to the author specific functions
     */
    public MOFAuthorListener(AuthorAccess authorAccess) {
        this.authorAccess = authorAccess;
        this.controller = authorAccess.getDocumentController();

//        try {
//            AuthorDocumentController docController = authorAccess.getDocumentController();
//            AuthorElement root = docController.getAuthorDocumentNode().getRootElement();
//            AuthorDocumentFragment fragment = docController.createDocumentFragment(root, true);
//
//            String authorXML = docController.serializeFragmentToXML(fragment);
//            filler = new XmlIdUtils(authorXML);
//        } catch (BadLocationException e) {
//            e.printStackTrace();
//        }
    }

    /**
     * @see ro.sync.ecss.extensions.api.AuthorListenerAdapter#attributeChanged(ro.sync.ecss.extensions.api.AttributeChangedEvent)
     */

    public void attributeChanged(AttributeChangedEvent e) {
//        String strictMode = authorAccess.getOptionsStorage().getOption("strictMode", "false");
//        if ("true".equals(strictMode)) {
//            // If the changed attribute is the "column_span" or "row_span" attribute of the "td"
//            // element then verify if the new value is an integer value
//            AuthorNode ownerAuthorNode = e.getOwnerAuthorNode();
//            if (ownerAuthorNode instanceof AuthorElement) {
//                AuthorElement ownerAuthorElement = (AuthorElement) ownerAuthorNode;
//                if ("td".equals(ownerAuthorElement.getLocalName())) {
//                    String attributeName = e.getAttributeName();
//                    if ("column_span".equals(attributeName) || "row_span".equals(attributeName)) {
//                        String spanValue = ownerAuthorElement.getAttribute(attributeName).getValue();
//                        try {
//                            Integer.parseInt(spanValue);
//                        } catch (NumberFormatException ex) {
//                            authorAccess.getWorkspaceAccess().showInformationMessage("The value " + spanValue + " of attribute " + attributeName +
//                                    " is not valid.");
//                        }
//                    }
//                }
//            }
//        }
    }

    /**
     * @see ro.sync.ecss.extensions.api.AuthorListenerAdapter#beforeContentDelete(ro.sync.ecss.extensions.api.DocumentContentDeletedEvent)
     */

    public void beforeContentDelete(DocumentContentDeletedEvent deleteEvent) {

//        String strictMode = authorAccess.getOptionsStorage().getOption("strictMode", "false");
//        if ("true".equals(strictMode)) {
//            // If the section title is deleted an error message will inform the user that the title
//            // is required.
//            if (!deleteEvent.isSimpleTextEdit()) {
//                AuthorNode changedNode = deleteEvent.getParentNode();
//                if (changedNode instanceof AuthorElement) {
//                    AuthorElement changedElement = (AuthorElement) changedNode;
//                    // Section element
//                    if ("section".equals(changedElement.getLocalName())) {
//                        AuthorElement[] titleElements = changedElement.getElementsByLocalName("title");
//                        // If the section has one "title" child element
//                        if (titleElements.length == 1) {
//                            // Find if the deleted element is the "title" one.
//                            AuthorDocumentFragment deletedFragment = deleteEvent.getDeletedFragment();
//                            List<AuthorNode> contentNodes = deletedFragment.getContentNodes();
//                            for (AuthorNode authorNode : contentNodes) {
//                                if (authorNode instanceof AuthorElement) {
//                                    if ("title".equals(((AuthorElement) authorNode).getLocalName())) {
//                                        String errorMessage = "The section must have a title.";
//                                        authorAccess.getWorkspaceAccess().getWorkspaceAccess().showErrorMessage(errorMessage);
//                                    }
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        }
        super.beforeContentDelete(deleteEvent);
    }

    /**
     * @see ro.sync.ecss.extensions.api.AuthorListenerAdapter#contentInserted(ro.sync.ecss.extensions.api.DocumentContentInsertedEvent)
     */

    public void contentInserted(DocumentContentInsertedEvent e) {
//        try {
//            String fragmentStringValue = controller.serializeFragmentToXML(e.getInsertedFragment());
//            authorAccess.getWorkspaceAccess().showErrorMessage(fragmentStringValue);
//            try{
//                fragmentStringValue = filler.fillIDinFragment(fragmentStringValue);
//            } catch (Exception ex){
//                ByteArrayOutputStream baos = new ByteArrayOutputStream();
//                PrintStream ps = new PrintStream(baos);
//                ex.printStackTrace(ps);
//                try {
//                    String content = baos.toString("UTF8");
//                    authorAccess.getWorkspaceAccess().showErrorMessage(ex.getLocalizedMessage() + "\n\n" + content);
//                } catch (UnsupportedEncodingException e1) {
//                    e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//                }
//            }
//            authorAccess.getWorkspaceAccess().showErrorMessage(fragmentStringValue);
//            if (fragmentStringValue != null){
//                AuthorDocumentFragment authorFragment = controller.createNewDocumentFragmentInContext(
//                        fragmentStringValue, authorAccess.getCaretOffset());
//
//            }
//        } catch (BadLocationException ex) {
//            authorAccess.getWorkspaceAccess().showErrorMessage(ex.getLocalizedMessage());
//        } catch (AuthorOperationException ex) {
//            authorAccess.getWorkspaceAccess().showErrorMessage(ex.getLocalizedMessage());
//        }
    }

    /**
     * @see ro.sync.ecss.extensions.api.AuthorListenerAdapter#contentDeleted(ro.sync.ecss.extensions.api.DocumentContentDeletedEvent)
     */

    public void contentDeleted(DocumentContentDeletedEvent e) {
//        AuthorNode node = e.getParentNode();
//        AuthorNode parentNode = node.getParent();
//        // For 'section' nodes the title text is rendered in the Outline
//        // (see customizeRenderingInformation method from SDFAuthorOutlineCustomizer)
//        // so we need to refresh the section node from the Outline when the title
//        // text has changed.
//        if ("title".equals(node.getName()) && "section".equals(parentNode.getName())) {
//            authorAccess.getOutlineAccess().refreshNodes(new AuthorNode[]{parentNode});
//        }
    }
}
